<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


use AuctionCore\Common\PhpRedis;

class QuestionScoreOrderLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $questionScoreOrderModel;

    public function _initialize()
    {
        $this->questionScoreOrderModel = D('QuestionScoreOrder', 'Model');

    }

    /**
     * 付费提问订单列表
     *
     * @param int   $page
     * @param int   $size
     * @param array $params
     */
    public function getQuestionScoreOrderList($page = 1, $size = 10, $params = [])
    {
        $map = [];
        $map['o.status'] = ['eq', $params['status']];
        if ($params['ord_num']) {
            $map['o.order_sn'] = $params['ord_num'];
        }
        if ($params['question_id']) {
            $map['o.question_id'] = $params['question_id'];
        }
        if ($params['user_id']) {
            $map['o.user_id'] = $params['user_id'];
        }
        if ($params['nick_name']) {
            $map['u.nick_name'] = $params['nick_name'];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['o.create_time'] = ['between', [$params['start_time'], $params['end_time']]];
        } elseif ($params['start_time']) {
            $map['o.create_time'] = ['gt', $params['start_time']];
        } elseif ($params['end_time']) {
            $map['o.create_time'] = ['lt', $params['end_time']];
        }
        if ($params['status'] == 1) {
            $map['q.status'] = 1;
        }
        $total = $this->questionScoreOrderModel->alias('o')
            ->join('left join clcw_question q ON o.question_id = q.question_id')
            ->join('left join clcw_user u ON o.user_id=u.user_id')
            ->where($map)
            ->count();
        $cols = 'o.id,o.order_sn,o.score,o.question_id,o.user_id,o.create_time,
                 o.status,o.return_time,u.nick_name,u.mobile';
        $list = $this->questionScoreOrderModel->alias('o')->field($cols)
            ->join('left join clcw_question q ON o.question_id = q.question_id')
            ->join('left join clcw_user u ON o.user_id=u.user_id')
            ->where($map)
            ->order('id DESC')
            ->page($page, $size)
            ->select();
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }

    /**
     * 获取提问订单详情
     *
     * @param int $id
     */
    public function getQuestionSocreOrderInfo($id = 0)
    {
        if (empty($id)) {
            return false;
        }
        return $this->questionScoreOrderModel->where(['id' => $id])->find();
    }

    /**
     * 提问积分订单退回
     *
     * @param $id
     */
    public function returnQuestionScore($id)
    {
        $order = $this->getQuestionSocreOrderInfo($id);

        $userInfo = D('ClcwUser', 'Logic')->getUserInfoByUid($order['user_id']);

        M('', 'clcw_', 'DB_CLCW')->startTrans();

        $redis = new PhpRedis(0);

        $keyName = getRedisKeyByCode('REDIS_KEY', '100000');

        $res1 = $redis->lPush($keyName, json_encode(
                [
                    'bizContent' =>
                        [
                            'scoreType' => 18,
                            'score'     => (int)$order['score'],
                            'remark'    => '退回问题悬赏积分',
                            'userId'    => (int)$userInfo['user_id']
                        ],
                    'bizType'    => 5
                ]
            )
        );

        $res2 = $this->questionScoreOrderModel->where(['id' => $id])->save(
            [
                'status'      => 2,
                'return_time' => NOW_TIME,
                'emp_id'      => $this->empId
            ]
        );

        $res3 = D('Question', 'Model')->where(['question_id' => $order['question_id']])
            ->save(
                [
                    'status'      => 0,
                    'update_time' => NOW_TIME
                ]
            );

        if ($res1 && $res2 && $res3) {
            M('', 'clcw_', 'DB_CLCW')->commit();
            return true;
        } else {
            M('', 'clcw_', 'DB_CLCW')->rollback();
            return false;
        }
    }
}